﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sprout.Core.Extensions
{
    public static class JsonExtensions
    {
        /// <summary>
        /// 将对象转成JSON字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="lowerCase">是否小驼峰</param>
        /// <param name="indented">是否缩进</param>
        /// <returns>JSON字符串</returns>
        public static string ToJsonString(this object obj, bool lowerCase = false, bool indented = false)
        {
            var settings = new JsonSerializerSettings();
            if (lowerCase)
            {
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }
            if (indented)
            {
                settings.Formatting = Formatting.Indented;
            }
            //var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            //options.Converters.Add(timeConverter);
            return JsonConvert.SerializeObject(obj, settings);
        }
        /// <summary>
        /// 将对象转成JSON字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="converters">JSON转换器</param>
        /// <param name="lowerCase">是否小驼峰</param>
        /// <param name="indented">是否缩进</param>
        /// <returns>JSON字符串</returns>
        public static string ToJsonString(this object obj, IEnumerable<JsonConverter> converters, bool lowerCase = false, bool indented = false)
        {
            var settings = new JsonSerializerSettings();
            if (lowerCase)
            {
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }
            if (indented)
            {
                settings.Formatting = Formatting.Indented;
            }
            (settings.Converters as List<JsonConverter>).AddRange(converters);
            return JsonConvert.SerializeObject(obj, settings);
        }
        /// <summary>
        /// 将JSON字符串转成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">JSON字符串</param>
        /// <returns></returns>
        public static T FromJsonString<T>(this string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return default(T);
            }
            else
            {
                return JsonConvert.DeserializeObject<T>(value);
            }
        }

        /// <summary>
        /// 将JSON字符串转换为匿名类型
        /// </summary>
        /// <typeparam name="T">匿名类型</typeparam>
        /// <param name="json">JSON字符串 </param>
        /// <param name="anonymousTypeObject">匿名类型</param>
        /// <returns></returns>
        public static T FromJsonString<T>(this string json, T anonymousTypeObject)
        {
            if (string.IsNullOrWhiteSpace(json))
            {
                return default(T);
            }
            return JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
        }

        /// <summary>
        /// 将JSON字符串转成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json">JSON字符串 </param>
        /// <param name="converters">JSON转换器</param>
        /// <returns></returns>
        public static T FromJsonString<T>(this string json, IEnumerable<JsonConverter> converters)
        {
            if (string.IsNullOrWhiteSpace(json))
            {
                return default(T);
            }
            var settings = new JsonSerializerSettings();
            (settings.Converters as List<JsonConverter>).AddRange(converters);
            return JsonConvert.DeserializeObject<T>(json, settings);
        }
    }
}
